/**
 * Created by Ji.wenjiang on 2016/8/25.
 */

/*
本地图像预览
 */
function fileReader($q, $log) {
  var onLoad = function (reader, deferred, scope) {
    return function () {
      scope.$apply(function () {
        deferred.resolve(reader.result);
      });
    };
  };
  var onError = function (reader, deferred, scope) {
    return function () {
      scope.$apply(function () {
        deferred.reject(reader.result);
      });
    };
  };
  var getReader = function (deferred, scope) {
    var reader = new FileReader();
    reader.onload = onLoad(reader, deferred, scope);
    reader.onerror = onError(reader, deferred, scope);
    return reader;
  };
  var readAsDataURL = function (file, scope) {
    var deferred = $q.defer();
    var reader = getReader(deferred, scope);
    reader.readAsDataURL(file);
    return deferred.promise;
  };
  return {
    readAsDataUrl: readAsDataURL
  };
}

 function getUrl() {
  return function (name, url) {
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (var i = 0; j = paraString[i]; i++) {
      paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[name.toLowerCase()];
    if (typeof(returnValue) == "undefined") {
      return "";
    } else {
      return returnValue;
    }
  }
}

function handleTime() {
  return function (handleTime) {
    if(handleTime.length<=10){
      var returnTime = parseInt(handleTime.split('-')[0] + handleTime.split('-')[1] + handleTime.split('-')[2]);
    }else {
      var returnTime =parseInt(handleTime.substr(0,4)+handleTime.substr(5,2)+handleTime.substr(8,2));
    }
    return returnTime;
  }
}



angular
  .module('inspinia')
  .factory('fileReader', fileReader)
  .factory('_undefined',function () {
    return function (someData) {
      if(someData==''||someData==undefined||someData==null){
        return true;
      }
    }
  })
  .factory('myToaster',['toaster',function (toaster) {
    return function (status, title, body) {
      toaster.pop({
        type: status,
        title: title,
        body: body,
        showCloseButton: true,
        timeout: 2200
      })
    }
  }])
  .factory('_undefinedToVoid',function () {
    return function (someData) {
      if(someData==''||someData==undefined||someData==null){
         return ' ';
      }else{
        return someData;
      }
    }
  })
  .factory('getUrl',getUrl)
  .factory('throttleFn',['$timeout',function ($timeout) {
    return function (fn, interval,$scope) {
      var __self = fn,    // 保存需要被延迟执行的函数引用
        timer,      // 定时器
        firstTime = true;    // 是否是第一次调用

      return function() {
        var args = arguments,
          __me = this;

        if (firstTime) {    // 如果是第一次调用，不需延迟执行
          __self.apply(__me, args);
          return firstTime = false;
        }

        if (timer) {    // 如果定时器还在，说明前一次延迟执行还没有完成
          return false;
        }

        timer = $timeout(function() {  // 延迟一段时间执行
          $timeout.cancel(timer);
          timer = null;
          __self.apply(__me, args);
        }, interval || 500 );
      };
    }
  }])
  .factory('handleTime', handleTime)
